

#include "./objsen.h"

//绘制ObjSen内obj
void sDrawSenObj(sObjSen*link)
{
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  (*temp->obj->draw)(temp->obj);
  temp = temp->next;
 }
}

void sDrawSenObjCam(sObjSen*link,sCam*cam)
{
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  if(temp->obj!=NULL)
  {
   if(temp->obj->draw==NULL)
   	sDrawObjCam(temp->obj,cam);
   else
   	(*temp->obj->draw)(temp->obj);
  }
  temp = temp->next;
 }
}

//向ObjSen添加obj
void sSenAddObj(sObjSen*link,
 sObj*obj)
{
 sObjSen*last = NULL;
 sObjSen*temp = link;
 //添加第一个
 if(temp->obj==NULL)
 {
  temp->obj = obj;
  return;
 }
 //循环遍历
 while(TRUE)
 {
  //第一次
  if(last==NULL)
  {
   //一个元素
   if(temp->next==NULL)
   {
    temp->next = 
    malloc(sizeof(sObjSen));
    temp = temp->next;
    break;
   }
   else
   {
    last = temp;
    temp = temp->next;
    continue;
   }
  }
  //遍历结束
  if(temp==NULL)
  {
   last->next = 
   malloc(sizeof(sObjSen));
   temp = last->next;
   break;
  }
  last = temp;
  temp = temp->next;
 }
 temp->obj = obj;
 temp->next = NULL;
}

//检测链接中数目
int sGetSenObjNum(sObjSen*link,int id)
{
 int index = 0;
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return index;
  //检测碰撞
  if(temp->obj!=NULL
    &&temp->obj->id==id)
  	index++;
  else if(id==-1)
  	index++;
  temp = temp->next;
 }
 return index;
}

//运行ObjSen
void sRunSenObj(sObjSen*link)
{
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  sRunObj(temp->obj);
  temp = temp->next;
 }
}

//调用事件
void sCallObjSenEve(sObjSen*link,int type,int p1,int p2)
{
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  if(temp->obj!=NULL)
  {
   if(temp->obj->eve!=NULL)
   {
    (*temp->obj->eve)
    (temp->obj,type,p1,p2);
   }
  }
  temp = temp->next;
 }
}

//检测点碰撞
sObj*sPointSenObj(sObjSen*link,
 int x,int y)
{
 sObj*result = NULL;
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return result;
  else
  	if(sPosCollObj(x,y,temp->obj))
  {
   if(temp->obj->view==TRUE)
   	result = temp->obj;
  }
  temp = temp->next;
 }
 return result;
}

//检测链接中全部碰撞
void sCollSenAllObj(sObjSen*link)
{
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  //检测碰撞
  if(temp->obj!=NULL)
  	sCollSenObj(link,temp->obj);
  temp = temp->next;
 }
}

//检测ObjSen碰撞
void sCollSenObj(sObjSen*link,
 sObj*obj)
{
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  //过滤自己和同组元素
  if(obj!=temp->obj)
  	if(obj->id!=
    temp->obj->id)
  	if(sObjCollObj(obj,temp->obj))
  {
   if(obj->coll!=NULL)
   	(*obj->coll)(obj,temp->obj);
  }
  temp = temp->next;
 }
}

//删除ObjSen内一个obj
void sDelSenOne(sObjSen**link,
 sObj*obj)
{
 int index = 0;
 sObjSen*last = NULL;
 sObjSen*temp = *link;
 while(TRUE)
 {
  if(temp==NULL)
  	return;
  
  //释放
  if(temp->obj==obj)
  {
   if(last!=NULL)
   	last->next = temp->next;
   else
   	*link = temp->next;
   
   sFreeObj(temp->obj);
   free(temp);
   temp = NULL;
   
   printf("sgame>delSenOne:%d\n",index);
   return;
  }
  index++;
  last = temp;
  temp = temp->next;
 }
}

//删除ObjSen内全部obj
void sDelSenAll(sObjSen*link)
{
 int index = 0;
 sObjSen*fSen = link;
 sObjSen*temp = link;
 while(TRUE)
 {
  if(temp==NULL)
  {
   printf("sgame>delSenAll:%d\n",index-1);
   return;
  }
  
  sFreeObj(temp->obj);
  fSen = temp->next;
  free(temp);
  temp = fSen;
  
  index++;
 }
}



